// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../component/ComponentBase";
import DataMgr from "../mgr/dataManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RankingList extends ComponentBase {

    item: cc.Node = null;
    content: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {

    }


    init(data, user) {

        // {uid: '658912536', score: '11', index: 1, avatar: '', nickname: ''}
        this.content.destroyAllChildren();
        console.log('data = ', data);
        console.log('user = ', user);
        for (let i = 0; i < data.length; i++) {
            const info = data[i];
            const item = cc.instantiate(this.item);
            if (i < 3) {
                item.getChildByName('rank_' + info.index).active = true;
            } else {
                item.getChildByName('rank_4').active = true;
                item.getChildByName('rank_4').getComponent(cc.Label).string = info.index.toString();
            }
            item.getChildByName('name').getComponent(cc.Label).string = info.uid;
            item.getChildByName('score').getComponent(cc.Label).string = info.score.toString();
            item.parent = this.content;
        }

        const item = this.item;
        if (user.rank == -1) {
            item.getChildByName('rank_0').active = true;
        } else if (user.rank < 4) {
            item.getChildByName('rank_' + (user.rank)).active = true;
        } else {
            item.getChildByName('rank_4').active = true;
            item.getChildByName('rank_4').getComponent(cc.Label).string = (user.rank).toString();
        }
        item.getChildByName('name').getComponent(cc.Label).string = DataMgr.Ins().uid;
        item.getChildByName('score').getComponent(cc.Label).string = user.score.toString();

    }


    closeBtnCallBack() {

        this.node.removeFromParent(true);
        this.node.destroy();
    }

    // update (dt) {}
}
